var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const ROOT_PATH = path.resolve(__dirname)
var config = {
  devtool: 'cheap-module-eval-source-map',
  entry: {
    app: ['react-hot-loader/patch', './app/index.jsx']
  },
  output: {
    path: __dirname + '/dist/',
    publicPath: '/dist/',
    filename: 'js/[name].js',
    chunkFilename: '[name].[chunkhash:5].chunk.js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    rules: [
      {
        enforce: 'pre',
        test: /\.(js|jsx)$/,
        loader: 'eslint-loader',
        exclude: '/node_modules/'
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader',
          fallback: 'style-loader'
        })
      },
      {test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=20&name=resource/[name].[ext]'},
      {test: /\.scss$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader!sass-loader',
          fallback: 'style-loader'
        })
      },
      {
        test: /\.js?$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader'
      },
      {
        test: /\.jsx?$/,
        exclude: '/(node_modules)/',
        loader: 'babel-loader'
      }
    ]
  },
  plugins: [
    // new webpack.DllReferencePlugin({
    //   manifest: require(path.resolve(ROOT_PATH, 'dist/lib', 'manifest.json')),
    //   context: ROOT_PATH
    // }),
    // 单独处理css

    new ExtractTextPlugin('css/[name].css'),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './app/index.html'
    }),
    new HtmlWebpackPlugin({
      filename: 'server_template.ejs',
      template: '!!ejs-compiled-loader!./app/server_template.ejs',
      inject: true,
      hash: true,
      chunks: ['vendor', 'app'],
      chunksSortMode: 'dependency',
      minify: {
        removeComments: true,
        collapseWhitespace: false
      }
    }),
    new webpack.HotModuleReplacementPlugin()
  ],
  devServer: {
    port: 8084,
    overlay: {
      errors: true
    },
    inline: true,
    compress: true,
    publicPath: '/dist/',
    historyApiFallback: {
      index: path.join(__dirname, './dist/index.html')
    },
    hot: true,
    proxy: {
      '/react_wp/': 'http://localhost:3334',
      '/user_action/': 'http://localhost:3334'
    }
  }
}
module.exports = config
